package com.victor.zjbj_android.yinhuan;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.blankj.utilcode.util.ToastUtils;
import com.victor.zjbj_android.R;
import com.victor.zjbj_android.bean.YhtypeListBean;
import com.victor.zjbj_android.utils.FastJsonTools;
import com.victor.zjbj_android.utils.MainUtils;
import com.victor.zjbj_android.utils.ZjbjManager;
import com.victor.zjbj_android.utils.retrofit.BaseSubscriber;
import com.victor.zjbj_android.utils.retrofit.ExceptionHandle;
import com.victor.zjbj_android.utils.retrofit.RetrofitUtils;
import com.victor.zjbj_android.yinhuan.adapter.MyExAdapter;
import com.victor.zjbj_android.bean.GroupBean;
import com.victor.zjbj_android.yinhuan.bean.ItemBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class YiHuanTypeActivity extends AppCompatActivity {
    ExpandableListView expandableListView;
    ArrayList<GroupBean> groupList ; //存储部门数据
    ArrayList<List> childList ; //存储装有人员信息的List


    public ArrayList<YhtypeListBean.DataBean.DataItemBean> alYhtype;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yi_huan_type);
        alYhtype = new ArrayList<>();
        initViews();
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initViews() {
        initData();
        expandableListView = (ExpandableListView) findViewById(R.id.view_exlist);
        //new一个适配器，就是之前我们创建的适配器，并且填入参数
        MyExAdapter adapter = new MyExAdapter(this,//Activity自带Context属性，所以传this进去就行了
                R.layout.item_group_exlist,//我们之前创建的显示部门名称的布局
                R.layout.item_exlist, //我们之前创建的显示部门人员信息的布局
                groupList, //刚刚创建的装有部门数据的List对象
                childList); //刚刚创建的装有人员信息的List的List对象

        //设置适配器，传入刚刚new的adapter适配的对象
        expandableListView.setAdapter(adapter);
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                Intent intent = new Intent();
                //把返回数据存入Intent
                intent.putExtra("result_id", alYhtype.get(childPosition).getId());
                intent.putExtra("result_str", alYhtype.get(childPosition).getTypename());
                //设置返回数据
                YiHuanTypeActivity.this.setResult(RESULT_OK, intent);

                //关闭Activity
                YiHuanTypeActivity.this.finish();
                return false;
            }

        });
    }

    private void initData() {
        //初始化变量
        groupList = new ArrayList();
        childList = new ArrayList();

        //添加部门信息
        groupList.add(new GroupBean("隐患类型"));


        RetrofitUtils mRetrofit1 = RetrofitUtils.getInstance(getApplicationContext()).createBaseApi();
        JSONObject jsonObject=new JSONObject();
        ZjbjManager tzsbManager=new ZjbjManager();

        tzsbManager.getYhtypeList(jsonObject.toString(), MainUtils.getToken(YiHuanTypeActivity.this), mRetrofit1, new BaseSubscriber() {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {

            }

            @Override
            public void onSuccess(JSONObject jsonObject) throws JSONException {
                YhtypeListBean requestBean= FastJsonTools.getBean(jsonObject.toString(),YhtypeListBean.class);
                if(requestBean.getCode().equals("0")){
                    if (alYhtype.size() > 0) {
                        alYhtype.clear();
                    }
                    alYhtype = (ArrayList<YhtypeListBean.DataBean.DataItemBean>) requestBean.getData().getDataItem();
                    childList.add(alYhtype);
                }else {
                    ToastUtils.showShort(requestBean.getInfo());
                }


            }
        });

    }

}
